package com.aabte.common.utils;

import com.aabte.common.annotation.HttpCode;
import com.aabte.common.beans.ResponseBean;
import com.aabte.common.constants.ErrorConstants;
import com.aabte.common.exception.ErrorCode;
import com.aabte.common.exception.OperationException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author Daniel
 */
public class ResponseBeanUtil {

    public static ResponseBean getResponseBean(ErrorCode ex) {
        ResponseBean responseBean = new ResponseBean();
        String code = ex.getCode();
        responseBean.setInnerCode(code);
        if (!StringUtils.isBlank(ex.getMessage())) {
            responseBean.setMessage(ex.getMessage());
        }
        return responseBean;
    }

    public static ResponseBean getResponseBean(String code) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.setInnerCode(code);
        return responseBean;
    }

    public static ResponseBean getResponseBean(Exception e) {
        ResponseBean responseBean;
        if (e instanceof ErrorCode) {
            responseBean = getResponseBean((ErrorCode) e);
        } else {
            responseBean = ResponseBean.fail(CommonMethod.getErrorMessage(e));
        }
        return responseBean;
    }

    public static ResponseBean setErrorResponse(HttpServletResponse response,
                                                ResponseBean responseBean,
                                                ErrorCode ex) {
        String code = ex.getCode();
        setErrorResponse(response, responseBean, code);
        if (!StringUtils.isBlank(ex.getMessage())) {
            responseBean.setMessage(ex.getMessage());
        }
        return responseBean;
    }

    public static ResponseBean setErrorResponse(HttpServletResponse response,
                                                ResponseBean responseBean,
                                                OperationException ex) {
        String code = ex.getCode();
        setErrorResponse(response, responseBean, code);
        if (!StringUtils.isBlank(ex.getMessage())) {
            responseBean.setMessage(ex.getMessage());
        }
        return responseBean;
    }

    public static ResponseBean setErrorResponse(HttpServletResponse response,
                                                ResponseBean responseBean,
                                                String code) {
        Map<String, HttpCode> codeHttpCodeMap = ErrorConstants.getCodeHttpCodeMap();
        HttpCode httpCode = codeHttpCodeMap.get(code);
        HttpStatus httpStatus = httpCode.httpStatus();
        String message = httpCode.message();
        if (StringUtils.isBlank(message)) {
            message = httpStatus.getReasonPhrase();
        }

        response.setStatus(httpStatus.value());
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);

        responseBean.setInnerCode(code);
        responseBean.setMessage(message);
        responseBean.setData(null);
        return responseBean;
    }

    public static ResponseBean setErrorResponse(HttpServletResponse response,
                                                ResponseBean responseBean,
                                                Exception e) {
        if (e instanceof OperationException) {
            return setErrorResponse(response, responseBean, (OperationException) e);
        }

        if (e instanceof ErrorCode) {
            return setErrorResponse(response, responseBean, (ErrorCode) e);
        }

        return setErrorResponse(response, responseBean, ErrorConstants.INTERNAL_SERVER_ERROR);
    }

}
